<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div#top{
            width: 100px;
            height: 100px;
            background-color: red;
        }
        div#bottom{
            width: 200px;
            height: 200px;
            background-color: blue;
        }
        div{
            margin: 20px;

        }
    </style>
    <script>
        window.onload=function(){
            var top=document.getElementById('top');
            var bottom=document.getElementById('bottom');
            top.ondragstart=function(event){
                var data=event.dataTransfer;
                data.setData('id',this.id);
            
            }
            
            bottom.ondragover=function(event){
                event.preventDefault();
                
            }
            bottom.ondrop=function(event){
                var id=event.dataTransfer.getData('id');
                //根据top id获取dom元素
                var source=document.getElementById(id);
                //将bottom替换掉source   第一个参数是新的 第二个参数是旧的
                this.parentNode.replaceChild(this,source);
                this.parentNode.appendChild(source);
            }

        }
    </script>
</head>
<body>
    <div id="top" draggable="true"></div>
    <div id="bottom"></div>

    </p>
</body>
</html>